﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SuperMap.Realspace;
using SuperMap.Data;


namespace Shine.Viewer
{
    // 定义更新书签的委托：通知外部UI保存书签
    // public delegate void UpdateCurrentBookmarkFile(三维书签 bm);
    // 定义更新当前场景的委托：通知外部UI切换场景
    // public delegate void UpdateCurrentScene(Camera ca);

    public partial class SingleBookmarkUserForm : UserControl
    {
        //private Scene currentScene = null;
        //private IForm activeForm = null;
        private RealspaceBookMarkStruck currentBMS;
        public RealspaceBookMarkStruck BookMarkStruck
        {
            get;
            set;
        }

        // 定义事件
        public event EventHandler<BookMarkEventArgs> UpdateBookmarkEventHandler;
        public event EventHandler<RealspaceBookMarkEventArgs> UpdateSceneEventHandler;

        public SingleBookmarkUserForm(RealspaceBookMarkStruck bk)
        {
            InitializeComponent();
            this.currentBMS = bk;
            this.BookMarkStruck = this.currentBMS;
            this.InitializeNewControl();

            // 增加点击image控件事件
            this.picBxBookmarkImage.Click += picBxBookmarkImage_Click;

            // 增加双击textbox控件事件
            this.tbxBookmarkCaption.MouseDoubleClick += tbxBookmarkCaption_MouseDoubleClick;

            // 增加textbox控件失去焦点事件
            this.tbxBookmarkCaption.LostFocus += tbxBookmarkCaption_LostFocus;

            // 增加textbox控件输入回车键事件
            this.tbxBookmarkCaption.KeyDown += tbxBookmarkCaption_KeyDown;
        }

        /// <summary>
        /// textbox控件输入回车键确认事件：
        /// 1. 记录下textbox的content
        /// 2. textbox的enable = false
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void tbxBookmarkCaption_KeyDown(object sender, KeyEventArgs e)
        {
            //throw new NotImplementedException();
            if(e.KeyCode == Keys.Enter)
            {
                string name = this.tbxBookmarkCaption.Text.Length > 0 ? this.tbxBookmarkCaption.Text:"未命名";
                this.currentBMS.bkname = name;
                this.BookMarkStruck = this.currentBMS;
                //this.tbxBookmarkCaption.Enabled = false;
            }

            // 保存修改后的名称，固化到xml
            三维书签 bk = new 三维书签
            {
                Altitude = this.BookMarkStruck.ca.Altitude.ToString(),
                AltitudeMode = this.GetAltitudeModeCode(this.BookMarkStruck.ca.AltitudeMode),
                Heading = this.BookMarkStruck.ca.Heading.ToString(),
                Tilt = this.BookMarkStruck.ca.Tilt.ToString(),
                Longitude = this.BookMarkStruck.ca.Longitude.ToString(),
                Latitude = this.BookMarkStruck.ca.Latitude.ToString(),
                Name = this.BookMarkStruck.bkname,
                UniCode = this.BookMarkStruck.bkunicode,
                ImagePath = this.BookMarkStruck.bkfile
            };
            
            this.OnFireUpdateBookmark(new BookMarkEventArgs(bk));
        }

        /// <summary>
        /// textbox控件失去焦点事件：
        /// 1. 记录下textbox的content
        /// 2. textbox的enable = false
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void tbxBookmarkCaption_LostFocus(object sender, EventArgs e)
        {
            string name = this.tbxBookmarkCaption.Text.Length > 0 ? this.tbxBookmarkCaption.Text:"未命名";
            this.currentBMS.bkname = name;
            this.BookMarkStruck = this.currentBMS;
            //this.tbxBookmarkCaption.Enabled = false;

            // 保存修改后的名称，固化到xml
            三维书签 bk = new 三维书签
            {
                Altitude = this.BookMarkStruck.ca.Altitude.ToString(),
                AltitudeMode = this.GetAltitudeModeCode(this.BookMarkStruck.ca.AltitudeMode),
                Heading = this.BookMarkStruck.ca.Heading.ToString(),
                Tilt = this.BookMarkStruck.ca.Tilt.ToString(),
                Longitude = this.BookMarkStruck.ca.Longitude.ToString(),
                Latitude = this.BookMarkStruck.ca.Latitude.ToString(),
                Name = this.BookMarkStruck.bkname,
                UniCode = this.BookMarkStruck.bkunicode,
                ImagePath = this.BookMarkStruck.bkfile
            };
            
            this.OnFireUpdateBookmark(new BookMarkEventArgs(bk));
        }

        private string GetAltitudeModeCode(AltitudeMode mode)
        {
            if (mode == AltitudeMode.ClampToGround)
                return "0";
            else if (mode == AltitudeMode.RelativeToGround)
                return "1";
            else if (mode == AltitudeMode.Absolute)
                return "2";
            else if (mode == AltitudeMode.RelativeToUnderground)
                return "3";
            else if (mode == AltitudeMode.AbsoluteUnderGround)
                return "4";
            else if (mode == AltitudeMode.ClampToObject)
                return "5";
            else if (mode == AltitudeMode.ModifyTerrain)
                return "6";
            else
                return "0";
        }

        /// <summary>
        /// 双击textbox控件：
        /// 1. 激活控件，处于可编辑状态
        /// 2. 清空内容框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void tbxBookmarkCaption_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.tbxBookmarkCaption.Enabled = true;
            this.tbxBookmarkCaption.Clear();
        }

        /// <summary>
        /// 初始化时，传入RealspaceBookMarkStruck对象
        /// 场景：初始化bookmark主面板、新建书签
        /// </summary>
        private void InitializeNewControl()
        {
            this.currentBMS = this.BookMarkStruck;
            this.picBxBookmarkImage.Tag = this.currentBMS;
            this.tbxBookmarkCaption.Text = this.currentBMS.bkname;
            picBxBookmarkImage.Image = Shine.Util.Drawing.ImageHelper.ImageFileToImage(BookMarkStruck.bkfile);
        }

        private void OnFireUpdateBookmark(BookMarkEventArgs bm)
        {
            if(this.UpdateBookmarkEventHandler != null)
            {
                this.UpdateBookmarkEventHandler(this,bm);
            }
        }

        private void OnFireUpdateScene(RealspaceBookMarkEventArgs ca)
        {
            if(this.UpdateSceneEventHandler != null)
            {
                this.UpdateSceneEventHandler(this,ca);
            }
        }

        /// <summary>
        /// 点击书签事件，实现：
        /// 1. 返回当前场景的范围，用于定位；
        /// 2. 记录当前书签，用于删除；
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void picBxBookmarkImage_Click(object sender, EventArgs e)
        {
            //this.tbxBookmarkCaption.BackColor = Color.LightYellow;
            this.OnFireUpdateScene(new RealspaceBookMarkEventArgs(this.BookMarkStruck));
            //this.tbxBookmarkCaption.BackColor = System.Drawing.SystemColors.Control;
        }

        public Double DWidth
        {
            get{
                return this.Width; 
            }
            set
            {
                this.DWidth = value;
            }
        }

        public Double DHeight
        {
            get
            {
                return this.Height;
            }
            set
            {
                this.DHeight = value;
            }
        }
    }
}
